package com.mkl.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkl.business.domain.Provider;
import com.mkl.business.mapper.ProviderMapper;
import com.mkl.business.service.ProviderService;
import com.mkl.business.vo.ProviderVo;
import com.mkl.system.common.Constant;
import com.mkl.system.common.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/8 9:58
 */
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService {

    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public DataGridView loadAllProvider(ProviderVo providerVo) {
        IPage<Provider> page = new Page<>(providerVo.getPage(), providerVo.getLimit());
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(providerVo.getProvidername()), "customername", providerVo.getProvidername());
        qw.like(StringUtils.isNotBlank(providerVo.getPhone()), "phone", providerVo.getPhone());
        qw.like(StringUtils.isNotBlank(providerVo.getConnectionperson()), "connectionperson", providerVo.getConnectionperson());
        providerMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @CachePut(cacheNames = "com.mkl.business.service.impl.ProviderServiceImpl", key = "#result.id")
    @Override
    public Provider saveProvider(Provider provider) {
        providerMapper.insert(provider);
        return provider;
    }

    @CachePut(cacheNames = "com.mkl.business.service.impl.ProviderServiceImpl", key = "#result.id")
    @Override
    public Provider updateProvider(Provider provider) {
        Provider selectById = providerMapper.selectById(provider.getId());
        BeanUtil.copyProperties(provider, selectById, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        providerMapper.updateById(selectById);
        return selectById;
    }

    @Override
    public DataGridView getAllAvailableProvider() {
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        List<Provider> providers = providerMapper.selectList(qw);
        return new DataGridView(providers);
    }

    @CacheEvict(cacheNames = "com.mkl.business.service.impl.ProviderServiceImpl", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Cacheable(cacheNames = "com.mkl.business.service.impl.ProviderServiceImpl", key = "#id")
    @Override
    public Provider getById(Serializable id) {
        return super.getById(id);
    }
}
